package com.example.administrator.student4;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import com.example.administrator.student4.Adapter.SearchbypersonAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * Created by 周千遇 on 2016/11/19.
 */
public class SearchByPerson extends Activity {
    TextView tv_number,tv_name,tv_sex,tv_class_a;
    ListView lv;
    JSONArray array;
    String name,subject,term;
    MyHandler myHandler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.searchbyperson);
        uiinit();
        myHandler = new MyHandler();
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if(bundle!=null){
            if(bundle.getInt("becon")==1){
                name = bundle.getString("name");
                subject = bundle.getString("subject");
                final JSONObject json = new JSONObject();
                try {
                    json.put("name",name);
                    json.put("subject",subject);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        String path = "http://10.0.2.2:8080/andorid/grade?request=findbynameandsubject";
                        try {
                            URL url = new URL(path);
                            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                            WebUtil.jsonOut(json,connection);
                            array = WebUtil.ioToJsonArray(connection.getInputStream());
                            myHandler.sendEmptyMessage(0x111);
                        } catch (java.io.IOException | JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
            if(bundle.getInt("becon")==2){
                name = bundle.getString("name");
                term = bundle.getString("term");
                final JSONObject json = new JSONObject();
                try {
                    json.put("name",name);
                    json.put("term",term);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        String path = "http://10.0.2.2:8080/andorid/grade?request=findbynameandterm";
                        try {
                            URL url = new URL(path);
                            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                            WebUtil.jsonOut(json,connection);
                            array = WebUtil.ioToJsonArray(connection.getInputStream());
                            myHandler.sendEmptyMessage(0x111);
                        } catch (java.io.IOException | JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        }

        lv = (ListView) findViewById(R.id.listView);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(SearchByPerson.this,ModifyActivity.class);
                intent.putExtra("name",tv_name.getText());
                try {
                    intent.putExtra("subject",array.getJSONObject(position).getString("subject"));
                    intent.putExtra("term",array.getJSONObject(position).getString("term"));
                    intent.putExtra("grade", array.getJSONObject(position).getString("grade"));
                    intent.putExtra("id", array.getJSONObject(position).getInt("id"));
                    startActivity(intent);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    public void uiinit(){
        tv_number = (TextView) findViewById(R.id.subject1);
        tv_name = (TextView) findViewById(R.id.name);
        tv_sex = (TextView) findViewById(R.id.sex);
        tv_class_a = (TextView) findViewById(R.id.class_a);
        lv = (ListView) findViewById(R.id.listView);
        Bundle bundle = getIntent().getExtras();
        if(bundle!=null){
            tv_number.setText(bundle.getString("number"));
            tv_name.setText(bundle.getString("name"));
            tv_sex.setText(bundle.getString("sex"));
            tv_class_a.setText(bundle.getString("class"));

        }
    }
    class MyHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 0x111){
                SearchbypersonAdapter adapter = new SearchbypersonAdapter(SearchByPerson.this,array);
                lv.setAdapter(adapter);
            }
        }
    }
}
